/*
 * Copyright@2014-2018版权所有
 *
 * 长沙市希尚网络科技有限公司
 *
 * 湘ICP备14001849号-1
 */

package com.jubaozan.c3.user;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yuanjie
 */
public class MyBatisPlusGenerator {

    /**
     * @author yuanjie
     */
    @FunctionalInterface
    public interface ConfigureHandler {
        /**
         * 配置
         *
         * @param globalConfig     -
         * @param dataSourceConfig -
         * @param packageConfig    -
         * @param strategyConfig   -
         */
        void beforeExecute(GlobalConfig globalConfig,
                           DataSourceConfig dataSourceConfig,
                           PackageConfig packageConfig,
                           StrategyConfig strategyConfig);
    }

    /**
     * 生成代码
     *
     * @param configureHandler -
     */
    public void generate(ConfigureHandler configureHandler) {
        // 代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        String outputDir = "D:/codeGen";
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(outputDir);
        globalConfig.setAuthor("yuanjie");
        globalConfig.setOpen(true);
        globalConfig.setIdType(IdType.AUTO);
        globalConfig.setFileOverride(true);
        globalConfig.setActiveRecord(true);
        globalConfig.setSwagger2(true);
        globalConfig.setEnableCache(true);
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setMapperName("%sMapper");
        globalConfig.setEntityName("%s");


        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mall?useUnicode=true&useSSL=false&characterEncoding=utf8");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername("mall");
        dataSourceConfig.setPassword("dba123456");


        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setEntity("domain");
        packageConfig.setMapper("mapper");
        packageConfig.setParent("com.jubaozan.c3.framework.mybatis.repository");

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();

        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return outputDir + "/mybatis/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        autoGenerator.setCfg(cfg);
        autoGenerator.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setRestControllerStyle(true);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setInclude(
                "fx_agent",
                "fx_agent_app",
                "fx_angent_extend",
                "fx_agent_pre",
                "fx_agent_channel",
                "fx_agent_order",
                "fx_agent_import_detail",
                "fx_agent_import_record",
                "fx_agent_import_resources"
        );
        strategy.setEntityBooleanColumnRemoveIsPrefix(true);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("fx");

        configureHandler.beforeExecute(globalConfig, dataSourceConfig, packageConfig, strategy);

        autoGenerator.setDataSource(dataSourceConfig);
        autoGenerator.setGlobalConfig(globalConfig);
        autoGenerator.setPackageInfo(packageConfig);
        autoGenerator.setStrategy(strategy);
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        autoGenerator.execute();
    }
}
